package com.starbaba.core.web.taglib.ace.component;

import java.io.Writer;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.components.Component;
import org.apache.struts2.util.MakeIterator;

import com.starbaba.core.web.translate.TranslateUtils;
import com.starbaba.core.web.translate.TranslateUtilsCacheDecorator;
import com.starbaba.core.web.translate.TranslateUtilsFactory;
import com.starbaba.core.web.translate.TranslateVO;
import com.opensymphony.xwork2.util.ValueStack;


public class AceCheckboxComponent extends Component {
	static Logger logger = Logger.getLogger(AceCheckboxComponent.class);

	private HttpServletRequest request;
	
	private Object list;
	private String name;
	private String value;
	

	public AceCheckboxComponent(ValueStack stack, HttpServletRequest request) {
		super(stack);
		this.request = request;
	}

	@Override
	public boolean start(Writer writer) {
		boolean result = super.start(writer);
		try {
			String targetName = this.getOutputMessage();
			if (StringUtils.isNotBlank(targetName)) {
				writer.write(targetName);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	public void evaluateExtraParams() {

	}

	public List<TranslateVO> getTranslateVOList() {
		try {
			TranslateUtils translateUtils = TranslateUtilsFactory.getInstance();
			if (null == translateUtils) {
				logger.error("TranslateUtils[definition=" + list + "]");
			}
			return new TranslateUtilsCacheDecorator(translateUtils).getCollection((String) list);
		} catch (Exception ex) {
			logger.error("TranslateUtils[definition=" + list + "]");
			logger.error(ex.getMessage());
		}
		return null;
	}

	public String getOutputMessage(){
		StringBuffer selectHtml = new StringBuffer();
		try{
				
		        if (list instanceof String) {
		        	String definition = (String) list;
		        	if (StringUtils.isNotBlank(definition) && definition.startsWith(TranslateUtils.SYSCODE_PREFIX)){
		        		List<TranslateVO> translateList = this.getTranslateVOList();
		        		
		        		selectHtml.append("<div class='checkbox'>");
		        		
		        		Set<String> checkedSet = new TreeSet<String>();
		        		if(StringUtils.isNotBlank(value)){
		        			String[] values = value.split(",");
		        			for(String checkedValue: values){
		        				checkedSet.add(checkedValue);
		        			}
		        		}
		        		
		        		for(TranslateVO item : translateList){
		        			selectHtml.append("<label>");
		        			selectHtml.append("<input class='ace ace-checkbox-2' type='checkbox' ");
		        			selectHtml.append(" name=").append(name);
		        			selectHtml.append(" value='").append(item.getCode()).append("'");
		        			if(checkedSet.contains(item.getCode())){
		        				selectHtml.append(" checked ");
		        			}
		        			selectHtml.append(">");
		        			selectHtml.append("<span class='lbl'>&nbsp;").append(item.getName()).append("</span>");
		        			selectHtml.append("</label>");
		        		}
		        		selectHtml.append("</div>");
		    		}
		        } else if (list instanceof Collection) {
		        	//TODO
		        } else if (MakeIterator.isIterable(list)) {
		        	//TODO
		        }
		}catch(Exception ex){
			logger.error("AceCheckboxComponent-异常:", ex);
			logger.error(ex.getMessage());
		}
		return selectHtml.toString();
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public Object getList() {
		return list;
	}

	public void setList(Object list) {
		this.list = list;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}
